package lsl.stack;

/**
 * 使用栈实现计算器
 *
 * @author Zed
 * @date 2021/12/29 22:20
 */
public class Calculator {
    public static void main(String[] args) {
        String expression = "3+9*6-2";
        ArrayStack2 mathStack = new ArrayStack2(10);
        ArrayStack2 operationStack = new ArrayStack2(10);
        //索引值 进行遍历索引
        int index = 0;
        //用来接收结果
        int res = 0;
        int num1 = 0;
        int num2 = 0;
        //操作符号
        int oper = 0;
        char c = ' ';

        while (true) {
            c = expression.substring(index, index + 1).charAt(0);
            //如果是符号
            if (operationStack.isOper(c)) {
                //如果当前符号栈不为空
                if (!operationStack.isEmpty()) {
                    //那么预入栈的元素要和符号栈栈顶元素比较优先级
                    //1.如果预入栈的元素优先级低于栈顶元素符号优先级
                    if (operationStack.priority(c) <= operationStack.priority(operationStack.peek())) {
                        num1 = mathStack.pop();
                        num2 = mathStack.pop();
                        res = operationStack.cal(num1, num2, operationStack.peek());
                        //1.1 将结果放入栈顶
                        mathStack.push(res);
                        operationStack.pop();
                    }
                    operationStack.push(c);
                } else {
                    //如果当前符号栈为空
                    operationStack.push(c);
                }
            } else {
                mathStack.push(c - 48);
            }
            index++;
            if (index == expression.length()) {
                break;
            }
        }
        //当表达式扫描完从数栈中push出结果
        while (true) {
            //如果符号栈为空，则计算到最后的结果, 数栈中只有一个数字【结果】
            if (operationStack.isEmpty()) {
                break;
            }
            num1 = mathStack.pop();
            num2 = mathStack.pop();
            oper = operationStack.pop();
            res = mathStack.cal(num1, num2, oper);
            //入栈
            mathStack.push(res);
        }
        //将数栈的最后数，pop出，就是结果
        int res2 = mathStack.pop();
        System.out.printf("表达式 %s = %d", expression, res2);
    }
}

/**
 * 用来表示栈
 */
class ArrayStack2 {
    //栈的大小
    private final int maxSize;
    //数组;用来模拟栈
    private final int[] stack;
    //栈顶
    private int top = -1;

    //构造器
    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    //栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //栈空
    public boolean isEmpty() {
        return top == -1;
    }

    //入栈
    public void push(int val) {
        if (isFull()) {
            System.out.println("栈已经满了");
        }
        top++;
        stack[top] = val;
    }

    //出栈
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈已经空了");
        }
        int val = stack[top];
        top--;
        return val;
    }

    //遍历栈
    public void list() {
        if (isEmpty()) {
            throw new RuntimeException("栈已经空了");
        }
        //需要从栈顶显示数据
        for (int i = top; i >= 0; i--) {
            System.out.println(stack[i]);
        }
    }

    //显示栈顶元素
    public int peek() {
        return stack[top];
    }

    //比较操作符的优先级
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }

    //判断是不是一个运算符
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    //根据操作符进行运算
    public int cal(int num1, int num2, int oper) {
        // res 用于存放计算的结果
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }
}
